package com.test.cordova.test;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import java.lang.reflect.Method;

import static android.content.Context.TELEPHONY_SERVICE;

/**
 * 移动数据网络 关闭 /打开
 * <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
 *  Not for use by third-party applications.
 *  系统软件可行  第三方应用行不通
 */

public class PhoneNetUitl {

    private static final  String TAG = PhoneNetUitl.class.getSimpleName();
    public static int count = 0;

    public static boolean setMobileDataEnable(Context activity ,boolean enable) {
        //5.0以上，禁用移动网络使用TelephonyManager#setDataEnabled
        //5.0以下，则是ConnectivityManager#setMobileDataEnabled
        Object object = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? activity.getSystemService(TELEPHONY_SERVICE) : activity.getSystemService(Context.CONNECTIVITY_SERVICE);
        String methodName = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? "setDataEnabled" : "setMobileDataEnabled";
        Method setMobileDataEnable;
        try {
            setMobileDataEnable = object.getClass().getDeclaredMethod(methodName, boolean.class);
            setMobileDataEnable.setAccessible(true);
            setMobileDataEnable.invoke(object, enable);
            return true;
        } catch (Exception e) {
            Log.d(TAG, "[setMobileDataEnable] error,exception:" + e.toString());
            e.printStackTrace();
            return false;
        }
    }


}
